<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');


class Admin extends CI_Controller {
	
	public function __construct() 
	{
		parent::__construct();
		
		$this->load->model('model_spc');
		$this->load->model('model_jpc');
		$this->load->model('model_auth');
		$this->load->model('model_admin');
		$this->model_auth->_must_logged_in('admin');
		
	}
	
	public function index () {	
		$this->load->view('header', 
			array('title'=> 'Admin Menu ITB Programming Contest 2011'));
		$this->load->view('admin/admin_menu');
		$this->load->view('footer');
	}

	public function manage_spc ($param='') {
		
		if ($param=='') {
			$data = array (
				'data' => $this->model_spc->_get_all()
				); 
		} 
		else /*if ($param=='not_valid')*/ {
			$data = array (
				'data' => $this->model_spc->_get_not_valid()
				); 		
		}
		$this->load->view('header', 
			array('title'=> 'Admin Menu ITB Senior Programming Contest 2011'));
		$this->load->view('admin/view_spc',$data);
		$this->load->view('footer');
	}
	
	public function edit_spc ($param='default') {
		if ($param=='default'){
				redirect('admin/','location');
			} else {
		$data = array (
			'userdata' => $this->model_spc->_get_userdata($param),
			);
		$this->load->view('header', 
			array('title'=> 'Admin Menu ITB Senior Programming Contest 2011'));
		$this->load->view('admin/edit_spc',$data);
		$this->load->view('footer');
	
	
	}}

	public function _valid_spc () {

		$this->valid = $this->form_validation;
		//RULE - nama ruas - nama org - parameter 
		
		$this->valid->set_rules('spc_nama_tim','Nama tim','required|min_length[4]|max_length[20]');
		$this->valid->set_rules('spc_nama_pt','Nama perguruan tinggi','required|min_length[3]|max_length[100]');
		$this->valid->set_rules('spc_alamat_pt','Alamat perguruan tinggi','required|min_length[3]|max_length[200]');
		$this->valid->set_rules('spc_nama_1','Nama anggota tim #1 (ketua tim)','required|min_length[3]|max_length[50]');
		$this->valid->set_rules('spc_nohp','Nomor ponsel ketua tim','required|is_natural');
		$this->valid->set_rules('spc_email','Alamat email ketua tim','required|valid_email');
		$this->valid->set_rules('spc_nama_2','Nama anggota tim #2','min_length[3]|max_length[50]');
		$this->valid->set_rules('spc_nama_3','Nama anggota tim #3','min_length[3]|max_length[50]');
		$this->valid->set_rules('spc_pembimbing','Nama pembimbing','required|min_length[3]|max_length[50]');
		
		$this->valid->set_error_delimiters('<div class="error">','</div>');
		$this->valid->set_message('matches','%s dan %s harus sama.');
		$this->valid->set_message('required','%s wajib diisi.');
		$this->valid->set_message('min_length','%s minimal  terdiri dari %s karakter.');
		$this->valid->set_message('max_length','%s maksimal terdiri dari %s karakter.');
		$this->valid->set_message('is_natural','%s hanya boleh terdiri dari angka saja.');
		$this->valid->set_message('valid_email','Email yang anda cantumkan tidak valid.');
		
		return ($this->valid->run()==FALSE)? FALSE :TRUE ;
		
	}
	
	public function spc_validasi ($param='') {
		if ($this->input->post()==NULL||$param=='') {
			redirect('admin/','location');
		} else if ($param=='sukses') {
			$this->load->view('redirect', array('message'=>'Pengubahan data sukses !'));
		} else {
			if ($this->_valid_spc()==FALSE) { 
				$this->edit_spc($this->input->post('spc_no_tim'));
			} else {
				$this->model_admin->_edit_spc($this->input->post(),$this->input->post('spc_no_tim'));
				if ($this->input->post('reset')==FALSE) {
					// $this->spc_validasi();
					$this->load->view('click_redirect', 
						array(
							'message'=>'Pengubahan data sukses !', 
							'redirect'=>'/admin/manage_spc', 
							'time'=>'2', 
							));
				} else /*if ()*/ {
					$new_pass= $this->model_admin->__reset_password('spc',$this->input->post('spc_no_tim'));
					$this->load->view('click_redirect', 
						array(
							'message'=>'Pengubahan data & password sukses !<br/>Password baru : '.$new_pass, 
							'redirect'=>'/admin/manage_spc', 
							'time'=>'0', 
							));
				
				}
			}
		}
	}
	

// JUNIOR
	
	public function manage_jpc ($param='') {
		
		if ($param=='') {
			$data = array (
				'data' => $this->model_jpc->_get_all()
				); 
		} 
		else /*if ($param=='not_valid')*/ {
			$data = array (
				'data' => $this->model_jpc->_get_not_valid()
				); 		
		}
		$this->load->view('header', 
			array('title'=> 'Admin Menu ITB Junior Programming Contest 2011'));
		$this->load->view('admin/view_jpc',$data);
		$this->load->view('footer');
	}
	
	public function edit_jpc ($param='default') {
		if ($param=='default'){
				redirect('admin/','location');
			} else {
		$data = array (
			'userdata' => $this->model_jpc->_get_userdata($param),
			);
		$this->load->view('header', 
			array('title'=> 'Admin Menu ITB Junior Programming Contest 2011'));
		$this->load->view('admin/edit_jpc',$data);
		$this->load->view('footer');
	
	}}

	public function _valid_jpc () {

		$this->valid = $this->form_validation;

		$this->valid->set_rules('jpc_nama','Nama anda','required|min_length[4]|max_length[50]');
		$this->valid->set_rules('jpc_nohp','Nomor ponsel anda','required|is_natural');
		$this->valid->set_rules('jpc_email','Alamat email anda','required|valid_email');
		$this->valid->set_rules('jpc_nama_sek','Nama sekolah','required|min_length[3]|max_length[100]');
		$this->valid->set_rules('jpc_alamat_sek','Alamat sekolah','required|min_length[3]|max_length[200]');
		$this->valid->set_rules('jpc_pembimbing','Nama pembimbing','required|min_length[3]|max_length[50]');
		
		// $this->valid->set_rules('kode_verifikasi','Kode verifikasi','required|callback_check_captcha');
		// $this->valid->set_rules('jpc_agreement','Persetujuan terhadap peraturan dan regulasi','required');
		
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		$this->valid->set_message('check_captcha','%s salah.');
		$this->valid->set_message('required','%s wajib diisi.');
		$this->valid->set_message('min_length','%s minimal  terdiri dari %s karakter.');
		$this->valid->set_message('max_length','%s maksimal terdiri dari %s karakter.');
		$this->valid->set_message('is_natural','%s hanya boleh terdiri dari angka saja.');
		$this->valid->set_message('valid_email','Email yang anda cantumkan tidak valid.');

		
		return ($this->valid->run()==FALSE)? FALSE :TRUE ;
		
	}
	
	public function jpc_validasi ($param='') {
		if ($this->input->post()==NULL||$param=='') {
			redirect('admin/','location');
		} else if ($param=='sukses') {
			$this->load->view('redirect', array('message'=>'Pengubahan data sukses !'));
		} else {
			if ($this->_valid_jpc()==FALSE) { 
				$this->edit_jpc($this->input->post('jpc_no_tim'));
			} else {
				$this->model_admin->_edit_jpc($this->input->post(),$this->input->post('jpc_no_tim'));
				if ($this->input->post('reset')==FALSE) {
					// $this->jpc_validasi();
					$this->load->view('click_redirect', 
						array(
							'message'=>'Pengubahan data sukses !', 
							'redirect'=>'/admin/manage_jpc', 
							'time'=>'2', 
							));
				} else /*if ()*/ {
					$new_pass= $this->model_admin->__reset_password('jpc',$this->input->post('jpc_no_tim'));
					$this->load->view('click_redirect', 
						array(
							'message'=>'Pengubahan data & password sukses !<br/>Password baru : '.$new_pass, 
							'redirect'=>'/admin/manage_jpc', 
							'time'=>'0', 
							));
				
				}
			}
		}
	}

	// public function reset_test() {
		// echo $this->model_admin->__reset_password('spc','1');
	
	// }
	

}
	
	